/**
 * @author luther.seven@gmail.com
 */

var liveXHR;

// XML Namespace resolver for XPath namespace resolution
function NSResolver(prefix) {
  var ns = {
    'georss' : 'http://www.georss.org/georss',
    'aws': 'http://www.aws.com/aws',
  };
  return ns[prefix] || null;
}

// Takes a definition document and populates the various DCTK controls
function handleLiveResult(doc)
{
    // extract information with XPath
	
	var main = doc.evaluate("//channel/title", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
	var mainDesc = doc.evaluate("//channel/description", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
	var poster = doc.evaluate("//channel/image/url", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
	
	var results = doc.evaluate("count(//channel/item)", doc, NSResolver, XPathResult.ANY_TYPE, null).numberValue;
	
	var title = new Array()
	for (var i = 1; i <= results; i++){
		title[i] = doc.evaluate("//channel/item[" + i + "]/title", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
		}
	var date = new Array()
	for (var i = 1; i <= results; i++){
		date[i] = doc.evaluate("//channel/item["+ i + "]/pubDate", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
		}
	var desc = new Array()
	for (var i = 1; i <= results; i++){
		desc[i] = doc.evaluate("//channel/item["+ i + "]/description", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
		}
    
    var dctk = connected.toolkit;
/*
	mainTitle = dctk.getElementById("mainTitle");
	mainTitle.text = "Daily Motion - "+mainDesc;
	
	queueTxt = dctk.getElementById("queueTxt");
	queueTxt.text = "Press UP/DOWN to change channels...";
	
	logo = dctk.getElementById("logo");
	logo.img.src = poster;
*/	

	var getTemp = desc[1].split('Temperature:<\/b> ')[1].split(' &deg;')[0];
	tempToday = dctk.getElementById("tempToday");
	tempToday.text = getTemp;
	connected.debug.print(getTemp);
	
	var titleEdit = title[1].split("from ");
	locationTxt = dctk.getElementById("locationTxt");
	locationTxt.text = titleEdit[1];
	
	dateTxt = dctk.getElementById("dateTxt");
	dateTxt.text = date[1];
	
	var getHumidity = desc[1].split('Humidity:<\/b> ')[1].split(' %')[0];
	humidityValue = dctk.getElementById("humidityValue");
	humidityValue.text = getHumidity+" %";
	
	var getWind = desc[1].split('Wind Speed:<\/b> ')[1].split('&nbsp;&nbsp;')[0];
	windValue = dctk.getElementById("windValue");
	windValue.text = getWind;
	
	var getPressure = desc[1].split('Pressure:<\/b> ')[1].split('&')[0];
	pressureValue = dctk.getElementById("pressureValue");
	pressureValue.text = getPressure+" \"";
	
	var getDew = desc[1].split('Dew Point:<\/b> ')[1].split('&deg;')[0];
	dewValue = dctk.getElementById("dewValue");
	dewValue.text = getDew+" deg";
	
	var getGusts = desc[1].split('Gusts:<\/b> ')[1].split('&nbsp;&nbsp;')[0];
	gustsValue = dctk.getElementById("gustsValue");
	gustsValue.text = getGusts;
	
	var getRain = desc[1].split('Rain Today:<\/b> ')[1].split('&')[0];
	rainValue = dctk.getElementById("rainValue");
	rainValue.text = getRain+" \"";
	
	var getImage = desc[1].split('img src=\"')[1].split('\" border')[0];
	iconToday = dctk.getElementById("iconToday");
	iconToday.img.src = getImage;
	
	connected.debug.print(main);
	connected.debug.print(title[1]);
	connected.debug.print(date[1]);
	//connected.debug.print(desc[1]);
	connected.debug.print(poster);

}

function onLiveXHRStateChange()
{
    if (liveXHR.readyState == 4) {
        if (liveXHR.status == 200) {
            handleLiveResult(liveXHR.responseXML);
        }
    }
}

function startLiveRequest()
{
	liveXHR = new XMLHttpRequest();
	liveXHR.open("GET", "http://api.wxbug.net/getLiveWeatherRSS.aspx?ACode="+apiKey+"&"+zipCode, true);
	liveXHR.overrideMimeType('text/xml');
	liveXHR.onreadystatechange = onLiveXHRStateChange;
	liveXHR.send(null); 
}